/**
 * @(#) ResupportMediator.as
 *
 * @date		2012. 09. 12.
 * @version		1.0
 * @author		Park, ju tae
 * @description	휴양소 지원금 신청 - Mediator
 * 				휴양소 지원금을 조회하고 신청하는 Presentation Logic을 관리
 */
package com.skt.common_hr.resupport.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.resupport.ResupportFacade;
	import com.skt.common_hr.resupport.events.ComboBoxYearEvent;
	import com.skt.common_hr.resupport.events.mmGwarekeyEvent;
	import com.skt.common_hr.resupport.model.CelebrationCodeProxy;
	import com.skt.common_hr.resupport.model.ResupportProxy;
	import com.skt.common_hr.resupport.model.vo.ResupportMoneyVO;
	import com.skt.common_hr.resupport.view.components.ResupportOverView;
	import com.skt.common_hr.resupport.view.components.popResupport;
	import com.wcl.utils.NumberUtil;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.IFlexDisplayObject;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class ResupportMediator extends OverViewMediator
	{
		public static const NAME:String = "resupportMoneyMediator";

		/**
		* 생성자
		*
		* @param
		* @return void
		*/
        public function ResupportMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }

		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		* 최초 한번만 불림
		*/
 		override protected function initializeView():void
 		{
			super.initializeView();
 		}
 		
		/***
		 * 활성화가 될때마다 불림.
		 */
		override protected function activatedView():void
		{
			super.activatedView();
 			loadList();
			setEventListener();
 			view.vsCompensation.selectedIndex = 0;
		}

		override protected function closedView():void
		{
			removeEventListener();
		}

 		private function setEventListener():void
 		{
 			// 휴양소 지원금 신청버튼
 			view.addEventListener(ResupportOverView.ResupportMoneyWrite, onClickNew);
			view.addEventListener(mmGwarekeyEvent.SELECT_DATA, onClickReadOnly);
			view.addEventListener(mmGwarekeyEvent.UPDATE_DATA, onClickModify);
			view.addEventListener(mmGwarekeyEvent.DELETE_DATA, onClickDelete);
			view.addEventListener(mmGwarekeyEvent.PRINT_DATA, onClickPrint);
			view.addEventListener(ComboBoxYearEvent.SELECT_YEAR_DATA, onComboYear);
 		}

 		private function removeEventListener():void
 		{
 			// 신청
 			view.removeEventListener(ResupportOverView.ResupportMoneyWrite, onClickNew);
			view.removeEventListener(mmGwarekeyEvent.SELECT_DATA, onClickReadOnly);
			view.removeEventListener(mmGwarekeyEvent.UPDATE_DATA, onClickModify);
			view.removeEventListener(mmGwarekeyEvent.DELETE_DATA, onClickDelete);
			view.removeEventListener(mmGwarekeyEvent.PRINT_DATA, onClickPrint);
			view.removeEventListener(ComboBoxYearEvent.SELECT_YEAR_DATA, onComboYear);
 		}


 
 		private function loadList():void  
 		{
 			var mProxy:ResupportProxy = facade.retrieveProxy(ResupportProxy.NAME) as ResupportProxy;
			mProxy.loadList(Variables.myPernr);
 		}
 		
 		private function onComboYear(event:ComboBoxYearEvent):void
 		{
 			var cbProxy:ResupportProxy = facade.retrieveProxy(ResupportProxy.NAME) as ResupportProxy;
			cbProxy.loadList(Variables.myPernr, event.comData);
 		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return basicInterestMessages.concat([
					
					ReqResupportMediator.CLOSED_REQUEST_VIEW,
					ResupportProxy.LOAD_RESUPPORTMONEY_LIST_COMPLETE,
					ResupportProxy.LOAD_RESUPPORTMONEY_LIST_FAILED,
					ResupportFacade.LOAD_SINCHUNG_LIST_COMPLETE,
					ResupportFacade.LOAD_SINCHUNG_LIST_FAILED,
					ResupportFacade.LOAD_RESUPPORTMONEY_DETAIL_COMPLETE,
					ResupportFacade.LOAD_RESUPPORTMONEY_DETAIL_FAILED,
					ResupportFacade.LOAD_CELEBRATION_LIST_COMPLETE,
					ResupportFacade.LOAD_CELEBRATION_LIST_FAILED,
					ResupportFacade.LOAD_CONGMONEY_SAVE_COMPLETE,
					ResupportFacade.LOAD_CONGMONEY_SAVE_FAILED,
					ResupportFacade.LOAD_CONGMONEY_DELETE_COMPLETE,
					ResupportFacade.LOAD_CONGMONEY_DELETE_FAILED,
					ResupportFacade.LOAD_CONGMONEY_SAVEFAL_FAILED,
					ReqResupportMediator.CLOSED_REQUEST_VIEW,
					CelebrationCodeProxy.LOAD_HOLIDAYS_FAILED,
					ResupportFacade.LOAD_RESUPPORTMONEY_PRINT_COMPLETE,
					ResupportFacade.LOAD_RESUPPORTMONEY_PRINT_FAILED
					
					]);
		}


		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		public var sGJAHR:String;
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName())
			{
				// 의료비 리스트  조회 Proxy 호출결과 처리
				case ResupportProxy.LOAD_RESUPPORTMONEY_LIST_COMPLETE :
					setListView(ResupportProxy(note.getBody()).mainList);
					view.acMedSubject = ResupportProxy(note.getBody()).acMedSubject;
					
					if(view.acMedYear.length <= 0) {
						view.acMedYear = ResupportProxy(note.getBody()).acMedYear;	
					}
					
					sGJAHR = ResupportProxy(note.getBody()).sGJAHR;
					break; 
					
				case ResupportFacade.LOAD_SINCHUNG_LIST_COMPLETE:
					view.sinChungList = ResupportProxy(note.getBody()).sinChungList;					
					onSinChung();
					break;
				case ResupportFacade.LOAD_SINCHUNG_LIST_FAILED:
					trace("LoadCongmoneyPrintFailed"); 
					break;	
				// 의료비 리스트 조회 실패시
				case ResupportProxy.LOAD_RESUPPORTMONEY_LIST_FAILED :
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_FAILED_LOAD_LIST", null, "ALERT_TITLE_SYSTEM_ERROR");
					break;

				// 재조회
				case ReqResupportMediator.CLOSED_REQUEST_VIEW:
					view.vsCompensation.selectedIndex = 0;
					loadList();
					break;

					
				case ResupportFacade.LOAD_RESUPPORTMONEY_DETAIL_COMPLETE:
					break;
				case ResupportFacade.LOAD_RESUPPORTMONEY_DETAIL_FAILED:
					trace("LoadRESUPPORTMONEYSelectLoadFailed");
					break;
				
				case ResupportFacade.LOAD_RESUPPORTMONEY_PRINT_COMPLETE:
					printListView(ResupportProxy(note.getBody()).printDataList, ResupportProxy(note.getBody()).printSubTabData);
					break;
				case ResupportFacade.LOAD_RESUPPORTMONEY_PRINT_FAILED:
					break;	
					
				// 리스트 조회 Proxy 호출결과 처리
				case ResupportFacade.LOAD_CELEBRATION_LIST_COMPLETE:
					setListView(ResupportProxy(note.getBody()).mainList);
					break;
				case ResupportFacade.LOAD_CELEBRATION_LIST_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_FAILED_LOAD_LIST", null, "ALERT_TITLE_SYSTEM_ERROR");
					break;
				case ResupportFacade.LOAD_CONGMONEY_DELETE_COMPLETE:
					//삭제후 조회
					loadList();
					break;
				case ResupportFacade.LOAD_CONGMONEY_DELETE_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_FAILED_DELETE", null, "ALERT_TITLE_SYSTEM_ERROR");
					break;


				default:
					break;
			}
		}
		 
		private static var dateFormatter:DateFormatter = new DateFormatter();
		public static function toKorString(src:Date):String
		{
			dateFormatter.formatString = "YYYY년 MM월 DD일";
			return dateFormatter.format(src);
		}
		
		public function onSinChung():void
		{
			var vo:ResupportMoneyVO =view.sinChungList.getItemAt(0) as ResupportMoneyVO; 
			
			view.resupportMoneyWriteView.lbTargetName.text = vo.ENAME;
			view.resupportMoneyWriteView.lbApplyDateYear.text = toKorString(vo.IDATE);
			view.resupportMoneyWriteView.lbApplyDateTxt.text = vo.WSTATX;
			view.resupportMoneyWriteView.lbTotalMoney.text =  NumberUtil.NumberFormat(vo.BETRG_Y);
			view.resupportMoneyWriteView.lbApplyMoney.text = NumberUtil.NumberFormat(vo.BETRG_Q);
			view.resupportMoneyWriteView.lbRemainMoney.text = NumberUtil.NumberFormat(vo.BETRG);
			view.resupportMoneyWriteView.lbBank.text = vo.BANKA;
			view.resupportMoneyWriteView.lbAccount.text = vo.BANKN;
			view.resupportMoneyWriteView.lbBankName.text = vo.EMFTX;
			
		}
		
 		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
 		private function setListView(list:ArrayCollection):void
 		{
 			view.resupportMoneyDataList = list;
 			commonFacade.setStatusMessage("STATUS_LOADED_LIST", [list.length.toString()]);
 			if(list.length == 0) {
 				view.showInitialTip();
 			}
 		}
 		
 		
 		private function printListView(list:ArrayCollection, list2:ArrayCollection):void
 		{
 			var popup:IFlexDisplayObject = PopUpManager.createPopUp(view, popResupport, true);
 			popResupport(popup).y = 20;
 			PopUpManager.centerPopUp(popup);
 			
 			var vo:ResupportMoneyVO =list.getItemAt(0) as ResupportMoneyVO; 
 			
 			popResupport(popup).lbENAME.text = vo.ENAME;
 			popResupport(popup).lbPERNR.text = String(vo.PERNR).replace('0', '');
 			popResupport(popup).lbORGTX.text = vo.ORGTX;
 			
 			popResupport(popup).lbBETRG_Y.text 	= NumberUtil.NumberFormat(vo.BETRG_Y) + '원 ';
			popResupport(popup).lbBETRG.text 	= NumberUtil.NumberFormat(vo.BETRG);
			popResupport(popup).lbHOWMUCH.text 	= NumberUtil.NumberFormat(vo.HOWMUCH);
			
			
			popResupport(popup).lbIDATE.text = vo.IDATE_F;
			popResupport(popup).lbENAME2.text = vo.ENAME;
 			
// 			popResupport(popup).printDataList = list;
			popResupport(popup).printSubTabData = list2;
			popResupport(popup).y = 20; 
 		}
 		
		/**
		 * 휴양소 지원금 조회버튼 이벤트 onClickModify
		 * @param
		 * @return void
		 */
 		private function onClickReadOnly(event:mmGwarekeyEvent):void
 		{
			var param:Object = new Object();
 			param.gwarekey  = event.key;
 			param.pernr = event.cpernr;
 			param.name  = Variables.myName;
 			param.write  = false;
			param.sGJAHR = sGJAHR;
 			executeReqView(param);
 		}

 		/** 
		 * 휴양소 지원금  신규 신청 버튼 처리
		 * @param
		 * @return void
		 */
		 
		public var I_GWAREKEY:String ='';
		public var I_GJAHR:String ='';
 		private function onClickNew(event:Event):void
 		{
 			
 			if(sGJAHR == '') {
				Alert.show('올해 지원대상자가 아닙니다.');
				return;
			}
 			view.resupportMoneyWriteView.lbHOWMANEY.text = '';
 			view.resupportMoneyWriteView.lbHOWMUCH.text = '';
 			var param:Object = new Object();
 			
 			commonFacade.setStatusMessage("");
			view.vsCompensation.selectedIndex = 1;
 			var writeMediator:ReqResupportMediator = facade.retrieveMediator(ReqResupportMediator.NAME) as ReqResupportMediator;

 			if(!writeMediator) {
				writeMediator = new ReqResupportMediator(view.resupportMoneyWriteView);
	 			facade.registerMediator(writeMediator);
			}
 			if(writeMediator) facade.sendNotification(ResupportFacade.LOAD_RESUPPORTMONEY_WRITE_INITIAL, param);
 			
			var proxy:ResupportProxy = facade.retrieveProxy(ResupportProxy.NAME) as ResupportProxy;
 			proxy.loadSave(I_GWAREKEY, Variables.myPernr);
 			
 		}
 		
 		
 		
 		/**
		 * 휴양소 지원금 신규 신청 및 수정 화면 실행
		 * @param
		 * @return void
		 */
 		private function executeReqView(param:Object):void
 		{
			commonFacade.setStatusMessage("");
			view.vsCompensation.selectedIndex = 1;
 			var writeMediator:ReqResupportMediator = facade.retrieveMediator(ReqResupportMediator.NAME) as ReqResupportMediator;

 			if(!writeMediator) {
				writeMediator = new ReqResupportMediator(view.resupportMoneyWriteView);
	 			facade.registerMediator(writeMediator);
			}
 			if(writeMediator) facade.sendNotification(ResupportFacade.LOAD_RESUPPORTMONEY_WRITE_INITIAL, param);
 		}

 		/**
		 * 휴양소 지원금 수정 버튼 클릭시 실행
		 * @param
		 * @return void
		 */
 		private function onClickModify(event:mmGwarekeyEvent):void
 		{
 			
			var param:Object = new Object();
 			param.gwarekey  = event.key;
 			param.pernr = event.cpernr;
 			param.name  = Variables.myName;
			param.write  	= true;
			param.sGJAHR = sGJAHR;
 			executeReqView(param);
 		}


 		/**
		 * 삭제 버튼 클릭시 실행
		 * @param
		 * @return void
		 */
 		private function onClickDelete(event:mmGwarekeyEvent):void
 		{
 			var mmProxy:ResupportProxy = facade.retrieveProxy(ResupportProxy.NAME) as ResupportProxy;
			mmProxy.deleteData(event.key);
 		}
 		
 		private function onClickPrint(event:mmGwarekeyEvent):void
 		{
 			var mmProxy:ResupportProxy = facade.retrieveProxy(ResupportProxy.NAME) as ResupportProxy;
			mmProxy.printData(event.key);
 		}

       private function get view():ResupportOverView
        {
            return viewComponent as ResupportOverView;
        }


	}
}